<?php
require_once '../config.php';
session_start();

// 检查管理员是否登录
if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $api_host = $_POST['api_host'];
    $api_path = $_POST['api_path'];
    $api_appcode = $_POST['api_appcode'];
    
    // 更新配置文件
    $config_content = file_get_contents('../config.php');
    $config_content = preg_replace("/define\('API_HOST',\s*'[^']*'\);/", "define('API_HOST', '$api_host');", $config_content);
    $config_content = preg_replace("/define\('API_PATH',\s*'[^']*'\);/", "define('API_PATH', '$api_path');", $config_content);
    $config_content = preg_replace("/define\('API_APPCODE',\s*'[^']*'\);/", "define('API_APPCODE', '$api_appcode');", $config_content);
    
    file_put_contents('../config.php', $config_content);
    
    $success = "配置已更新";
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .sidebar {
            min-height: 100vh;
            background-color: #343a40;
            color: white;
        }
        .sidebar .nav-link {
            color: rgba(255,255,255,.75);
        }
        .sidebar .nav-link:hover {
            color: rgba(255,255,255,1);
        }
        .sidebar .nav-link.active {
            color: white;
            background-color: rgba(255,255,255,.1);
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-3 col-lg-2 px-0 sidebar">
                <div class="p-3">
                    <h4>后台管理系统</h4>
                    <hr>
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link active" href="index.php">API配置</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="logout.php">退出登录</a>
                        </li>
                    </ul>
                </div>
            </div>
            
            <!-- 主内容区 -->
            <div class="col-md-9 col-lg-10 p-4">
                <h2>API配置</h2>
                <hr>
                
                <?php if(isset($success)): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <div class="card">
                    <div class="card-body">
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label for="api_host" class="form-label">API主机地址</label>
                                <input type="text" class="form-control" id="api_host" name="api_host" value="<?php echo API_HOST; ?>" required>
                            </div>
                            <div class="mb-3">
                                <label for="api_path" class="form-label">API路径</label>
                                <input type="text" class="form-control" id="api_path" name="api_path" value="<?php echo API_PATH; ?>" required>
                            </div>
                            <div class="mb-3">
                                <label for="api_appcode" class="form-label">AppCode</label>
                                <input type="text" class="form-control" id="api_appcode" name="api_appcode" value="<?php echo API_APPCODE; ?>" required>
                            </div>
                            <button type="submit" class="btn btn-primary">保存配置</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html> 